<template>
  <div>
    <navbar theme="light" :title="title" rightItemText="保存" v-on:rightItemClick="save"></navbar>
    <div class="form">
      <div class="form-row">
        <xinput placeholder="" :value.sync="value" :type="type"
                :maxLength="maxLength" class="form-row-control"></xinput>
      </div>
    </div>
    <wxc-loading :show="isLoadingShow" ></wxc-loading>
  </div>
</template>
<script>
import { WxcLoading } from 'weex-ui'
require('./include/page-xdom')
let api = require('./include/api')
let modal = weex.requireModule('modal')
let navigator = require('./include/xnavigator')
let queryParser = require('./include/queryparser')
module.exports = {
  mixins: [],
  components: {
    navbar: require('./components/navbar.vue'),
    xinput: require('./components/input.vue'),
    WxcLoading
  },
  data () {
    return {
      title: '',
      notNull: 1,
      type: 'text',
      maxLength: 10000,
      field: '',
      value: '',
      isLoadingShow: false
    }
  },
  created () {
    let params = queryParser(weex.config.bundleUrl)
    this.title = decodeURIComponent(params.title)
    if (this.notNull) {
      this.notNull = parseInt(this.notNull)
    }
    if (params.type) {
      this.type = params.type
    }
    if (params.maxLength) {
      this.length = parseInt(params.maxLength)
    }
    this.field = params.field
    this.value = decodeURIComponent(params.value)
  },
  methods: {
    validate () {
      if (!this.params.phone || this.params.phone.length !== 11) {
        return false
      }
      if (!this.params.verifyCode || this.params.verifyCode.length !== 6) {
        return false
      }
      return true
    },
    save () {
      if (this.notNull && this.value.length === 0) {
        modal.alert({message: this.title + '不能为空'})
        return
      }
      this.isLoadingShow = true
      api.user.updateField({
        field: this.field,
        value: this.value
      }).done(rs => {
        modal.toast({message: this.title + '保存成功', duration: 0.5})
        navigator.pop()
      }).always(() => {
        this.isLoadingShow = false
      })
    }
  }
}
</script>
<style lang="scss">
  @import "./sass/var";
  @import "./sass/mixin";
  @import "./sass/icon";
  @import "./sass/buttons";
  @import "./sass/activeable";
  @import "./sass/form";
  @import "./sass/utilities";

  .container {
    background-color: #FFFFFF;
  }

  .form-row-text {
    margin-top: 30px;
    align-items: center;
    justify-content: space-between;
    flex-direction: row;
    padding: 0 15px;
  }

  .form {
    padding: 0 40px;
    margin-top: 60px;
  }

</style>
